﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;


namespace FYPHP
{
   
    public partial class SearchMember : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);
  
        protected void Page_Load(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = -1;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SqlCommand cmdMember;
            SqlDataReader dtrMember;
            conn.Open();

            if (txtIC.Text == "" && txtUserName.Text == "" && txtEmail.Text == "" && txtPosition.Text == "")
            {
                MultiView1.ActiveViewIndex = 1;
            }
            else
            {
                String query = "SELECT * FROM Users u WHERE u_gender IN('Male','Female')";

                if(txtIC.Text!="")
                    query += " AND u_ic =" + txtIC.Text;

                if (txtUserName.Text != "")
                    query += " AND username LIKE '" + txtUserName.Text + "'";

                if (txtEmail.Text != "")
                    query += " AND u_email ='" + txtEmail.Text + "'";

                if (txtPosition.Text != "")
                    query += " AND u_position ='" + txtPosition.Text + "'";


                cmdMember = new SqlCommand(query, conn);
                int num = Convert.ToInt32(cmdMember.ExecuteScalar());

                if (num != 0) //if member available
                {
                    dtrMember = cmdMember.ExecuteReader();

                    ItemList.DataSource = dtrMember;
                    ItemList.DataBind();
                    dtrMember.Close();

                    MultiView1.ActiveViewIndex = 0;
                }
                else //if member not available
                {
                    MultiView1.ActiveViewIndex = 1;
                }

                conn.Close();
            }


        }

   
    }
}